<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '格知汇GACC',
    navigationStyle: 'custom',
  },
}
</route>
<template>
  <view class="entire">
    <image
      style="z-index: -10; position: fixed; top: 0; width: 100vw; height: 100vh"
      :src="listBg"
      mode="aspectFill"
    ></image>
    <view class="div1">
      <Backbar title="我的收藏" />
      <!-- 一级选择器 -->
      <wht-tabs
        v-model="pageData.tab1"
        :list="pageData.tabList1"
        @change="handleTab1"
        bgColor="#e5effd"
        color="#898889"
        active-color="#254872"
        line-color="#254872"
        :font-size="27"
        :isRound="true"
        :padding="70"
      />
    </view>
    <scroll-view
      ref="scrollContainer"
      scroll-y="true"
      class="div2"
      enable-flex="true"
      :style="{ height: pageData.div2Height + 'px' }"
      @scrolltolower="loadMoreData"
    >
      <!-- 空显示 -->
      <view
        v-if="
          (pageData.tab1 === 1 && pageData.courseList.length === 0) ||
          (pageData.tab1 === 2 && pageData.knowledgeList.length === 0) ||
          (pageData.tab1 === 0 &&
            pageData.courseList.length === 0 &&
            pageData.knowledgeList.length === 0)
        "
        style="
          display: flex;
          align-items: center;
          justify-content: center;
          width: 100%;
          height: 100%;
        "
      >
        <image src="/static/images/icons/imgNone.png" style="width: 309rpx; height: 259rpx" />
      </view>

      <view v-if="pageData.tab1 === 1 || pageData.tab1 === 0">
        <view
          v-for="(item, index) in pageData.courseList"
          :key="index"
          @click="navToCourseInfo(item)"
        >
          <TiniCourseItem :course="item" />
        </view>
      </view>
      <view v-if="pageData.tab1 === 2 || pageData.tab1 === 0">
        <view
          v-for="(item, index) in pageData.knowledgeList"
          :key="index"
          @click="navToRichText(item)"
        >
          <KnowledgeItem :knowledge="item" />
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { getCollectionList, getCollectionKnowledgeList } from '@/service/collection/collection'
import TiniCourseItem from '@/components/CourseItem/TiniCourseItem.vue'
import KnowledgeItem from '@/components/KnowledgeItem/KnowledgeCollectionItem.vue'
import { listBg } from '@/static/imgMap'

const pageData = reactive({
  tab1: 0,
  tabList1: ['全部', '课程', '知识'],
  div2Height: 0,
  courseList: [],
  knowledgeList: [],
  loading: false,
  page: 1,
  limit: 10,
  totalCount: 0,
})

onMounted(() => {
  uni.getSystemInfo({
    success(res) {
      const screenHeight = res.windowHeight
      uni
        .createSelectorQuery()
        .select('.div1')
        .boundingClientRect((data) => {
          pageData.div2Height = screenHeight - data.height
        })
        .exec()
    },
  })
  fetchCollectionData()
})

const fetchCollectionData = async () => {
  pageData.loading = true
  try {
    if (pageData.tab1 === 1 || pageData.tab1 === 0) {
      const response = await getCollectionList({
        rtype: 0,
        page: pageData.page,
        limit: pageData.limit,
      })
      pageData.courseList = response.data.items
      pageData.totalCount = response.data.totalCount
    }
    if (pageData.tab1 === 2 || pageData.tab1 === 0) {
      const response = await getCollectionKnowledgeList(pageData.page, pageData.limit)
      pageData.knowledgeList = response.data.items
      pageData.totalCount = response.data.totalCount
    }
    pageData.page += 1
  } catch (error) {
    console.error('Error fetching collection data:', error)
  } finally {
    pageData.loading = false
  }
}

const loadMoreData = async () => {
  if (
    pageData.loading ||
    pageData.totalCount <= pageData.courseList.length + pageData.knowledgeList.length
  ) {
    return
  }

  pageData.loading = true

  try {
    if (pageData.tab1 === 1 || pageData.tab1 === 0) {
      const response = await getCollectionList({
        rtype: 0,
        page: pageData.page,
        limit: pageData.limit,
      })
      pageData.courseList.push(...response.data.items)
    }
    if (pageData.tab1 === 2 || pageData.tab1 === 0) {
      const response = await getCollectionKnowledgeList(pageData.page, pageData.limit)
      pageData.knowledgeList.push(...response.data.items)
    }
    pageData.page += 1
  } catch (error) {
    console.error('Error loading more data:', error)
  } finally {
    pageData.loading = false
  }
}

const handleTab1 = (index) => {
  pageData.tab1 = index
  pageData.page = 1
  pageData.courseList = []
  pageData.knowledgeList = []
  fetchCollectionData()
}

const navToCourseInfo = (data) => {
  uni.navigateTo({
    url:
      '/pages/courseCenter/CourseInfo/CourseInfo?data=' + encodeURIComponent(JSON.stringify(data)),
  })
}

const navToRichText = (data) => {
  const updatedData = { ...data, isCollect: true }
  uni.navigateTo({
    url:
      '/pages-sub/knowledgeRichText/knowledgeRichText?data=' +
      encodeURIComponent(JSON.stringify(updatedData)),
  })
}
</script>

<style scoped lang="scss">
.entire {
  width: 100%;
  .div1 {
    background-color: rgb(37, 73, 115);
    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgb(37, 73, 115);
      width: 100%;
      height: 120rpx;
      color: #fff;
      font-size: large;
    }
  }
  .div2 {
    display: flex;
    flex-direction: column;
    width: 100%;
    overflow-y: scroll;
  }
}
</style>

<style scoped lang="scss">
.entire {
  width: 100%;
  .div1 {
    background-color: rgb(37, 73, 115);
    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgb(37, 73, 115);
      width: 100%;
      height: 120rpx;
      color: #fff;
      font-size: large;
    }
  }
  .div2 {
    display: flex;
    flex-direction: column;
    width: 100%;
    overflow-y: scroll;
  }
}
</style>
